package com.ag.work.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.xml.crypto.Data;

import org.springframework.stereotype.Service;

import com.ag.bean.TbDisposalAdvice;
import com.ag.bean.TbEvent;
import com.ag.bean.TbExpert;
import com.ag.bean.TbExpertArea;
import com.ag.bean.TbUser;
import com.ag.common.IDServiceImpl;
import com.ag.common.IDUtil;
import com.ag.dao.TbDisposalAdviceDAO;
import com.ag.dao.TbExpertDAO;
import com.ag.lwc.service.impl.LogService;

import com.ag.work.service.IAdviceApplyService;
import com.ag.zhh.action.ContactLogAction;
import com.ag.zhh.service.ContactLogService;
import com.ag.zlw.dao.TbExpertAreaDAO;

/**
 * 
 * @author dgm
 * 申请处置建议
 *
 */
@Service("adviceApplyServiceImpl")
public class AdviceApplyServiceImpl implements IAdviceApplyService {

	
	@Resource(name="ZLWTbExpertAreaDAO")
	private TbExpertAreaDAO tbExpertAreaDAO;
	
	@Resource(name="TbExpertDAO")
	private TbExpertDAO tbExpertDAO;
	
	@Resource(name="iDServiceImpl")
	private IDServiceImpl idService;
	
	@Resource(name="TbDisposalAdviceDAO")
	private TbDisposalAdviceDAO tbDisposalAdviceDAO;
	
	@Resource(name="ContactLogService")
	private ContactLogService messageSend;
	
	@Resource(name="LogService")
	private LogService logService;
	/**
	 * 返回全部专家领域
	 */
	@Override
	public List<TbExpertArea> findExpertArea() {
		System.out.println("这是findExpertArea_service方法");
		return tbExpertAreaDAO.findAll();
	}

	/**
	 * @param expertAreaIdString 专家领域的id
	 * @return   专家对象LIST
	 */
	@Override
	public List<TbExpert> findExpertByArea(String expertAreaIdString) {
		String hql="from TbExpert t where t.tbExpertArea.expareaId="+expertAreaIdString+" order by t.expName desc";
		return tbExpertDAO.getHibernateTemplate().find(hql);
	}

	@Override
	public void applyAdvice(String eventIdString, String adviceExpertIds,
			String lastestTime, String applyString, String applyPersonId) {
		System.out.println("这是applyAdvice_serviceimpl方法");
		Date lastTime =  StringToDate(lastestTime);			//时间转成date类型
		List<TbUser> adviceUserList=findTbListByString(adviceExpertIds);	//通过一个专家id的组查找专家对象LIST
		/*
		 * 查申请人
		 */
		String userHql = "from TbUser t where t.UId in ('"+applyPersonId+"')";
		TbUser tbApplyUser = (TbUser) tbExpertDAO.getHibernateTemplate().find(userHql).get(0);
		/*
		 * 查事件对象
		 */
		String eventHql ="from TbEvent t where t.EId="+eventIdString;
		TbEvent tbEvent = (TbEvent)tbExpertDAO.getHibernateTemplate().find(eventHql).get(0);
		
		/*
		 * 批量处理
		 */
		for(TbUser t:adviceUserList){
		TbDisposalAdvice tbDisposalAdvice = new TbDisposalAdvice();
		
		tbDisposalAdvice.setDaId(idService.findTablePK(IDUtil.TB_DISPOSAL_ADVICE));
		tbDisposalAdvice.setTbEvent(tbEvent);
		tbDisposalAdvice.setDaApplyTime(new Date());
		tbDisposalAdvice.setDaLastestTime(lastTime);
		tbDisposalAdvice.setTbUserByApplyUId(tbApplyUser);
		tbDisposalAdvice.setTbUserByAdviceUId(t);
		tbDisposalAdvice.setA(IDUtil.PRESERVE);
		tbDisposalAdvice.setB(applyString);
		tbDisposalAdvice.setC(DisposalStatusServiceImpl.ADVICE_STATUS_SQ);
		
		tbDisposalAdviceDAO.save(tbDisposalAdvice);

		}
	}
	
	/**
	 * 
	 * @param s   时间类型 的string 
	 * @return		date类型
	 */
	public Date StringToDate(String s){
		System.out.println(s);
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date currentTime = new Date();
		try {
			currentTime = format.parse(s);
			System.out.println("currentTime="+currentTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return currentTime;
	}

	
	@Override
	public void sendMessageAndLog(String eventIdString, String adviceExpertIds, String applyString, String applyPersonId){
		System.out.println("这是sendMessageAndLog_serviceImpl方法");
		List<TbUser> adviceUserList=findTbListByString(adviceExpertIds);	//通过一个专家id的组查找专家对象LIST
		String userHql = "from TbUser t where t.UId in ('"+applyPersonId+"')";
		TbUser tbApplyUser = (TbUser) tbExpertDAO.getHibernateTemplate().find(userHql).get(0);
		List<String> ctCalledList = new ArrayList<String>(adviceUserList.size());
		List<String> ctCalledUIdList = new ArrayList<String>(adviceUserList.size());
		List<String> ctCalledNumberList = new ArrayList<String>(adviceUserList.size());
		for(TbUser t:adviceUserList){
			ctCalledList.add(t.getUName());
			ctCalledUIdList.add(t.getUId());
			ctCalledNumberList.add(t.getUPhone());
		}
		
		String[] ctCalled= new String[ctCalledList.size()];
		ctCalledList.toArray(ctCalled);
		String[] ctCalledUId = new String[ctCalledUIdList.size()];
		ctCalledUIdList.toArray(ctCalledUId);
		String[] ctCalledNumber = new String[ctCalledNumberList.size()];
		ctCalledNumberList.toArray(ctCalledNumber);
		/*
		 * 发短信
		 */
		messageSend.addMessageContactLogByEvent(tbApplyUser.getUName(), ctCalled, ctCalledUId, ctCalledNumber, applyString, eventIdString);
		/*
		 * 存入系统日志
		 */
		logService.saveAdviceLog("增加处置建议", eventIdString);
	}


	/**
	 * @param adviceExpertIds 一个'XX','XX','XX','的字符串
	 */
	public List<TbUser> findTbListByString(String adviceExpertIds){
		String ae = adviceExpertIds.substring(0, (adviceExpertIds.length()-2));		//转成hql中的字符串
		/*
		 * 查专家user对象
		 */
		String hql= "from TbUser t where t.UId in ("+ae+") where t.a="+IDUtil.PRESERVE;
		List<TbUser> adviceUserList = tbExpertDAO.getHibernateTemplate().find(hql);
		return adviceUserList;
	}

}
